#include "MasterConfig.hpp"
#include "cybertron/core/Log.hpp"
#include "cybertron/core/JsonReader.hpp"
#include <typeinfo>
MasterConfig::MasterConfig()
{

}
MasterConfig::~MasterConfig()
{

}

bool MasterConfig::load(const std::string & config, const bool & isFile)
{
	using namespace cybertron;
	clear();
	json jsonNode;
	if (isFile){
		if (!JsonReader::loadFile(jsonNode, config)){
			logError("Config file not found: %s", config.c_str());
			return false;
		}
	}
	else{
		if (!JsonReader::loadString(jsonNode, config)){
			logError("Config string parses failed.");
			return false;
		}
	}

	mTaskId = JsonReader::getString(jsonNode, "taskId");
	mTaskTimeOut = JsonReader::getInt(jsonNode, "timeout");
	mCaseId = JsonReader::getString(jsonNode, "caseId");
	mCaseName = JsonReader::getString(jsonNode, "caseName");
	mWorldConfigJson = JsonReader::getString(jsonNode, "config");

	if (JsonReader::hasValue(jsonNode, "nodes")) {
		const auto& jsonNodeWorkList = JsonReader::getChild(jsonNode, "nodes");
		for (size_t i = 0; i < jsonNodeWorkList.size(); ++i)
		{
			std::string nodeWorkList = JsonReader::getString(jsonNodeWorkList[i]);
			if (nodeWorkList != std::string(" "))
			{
				mNodeWorkList.push_back(nodeWorkList.c_str());
			}
		}
	}
	auto pp = mNodeWorkList;
	return true;
}

string MasterConfig::getTaskId() const
{
	return mTaskId;
}

int MasterConfig::getTaskTimeOut() const
{
	return mTaskTimeOut;
}

string MasterConfig::getCaseId() const
{
	return mCaseId;
}

string MasterConfig::getCaseName() const
{
	return mCaseName;
}

string MasterConfig::getWorldConfigJson() const
{
	return mWorldConfigJson;
}

vector<std::string> MasterConfig::getNodeWorkList() const
{
	return mNodeWorkList;
}

void MasterConfig::clear()
{
	//mTaskId.clear();
	//mTaskTimeOut = 0;
	//mCaseId.clear();
	//mCaseName.clear();
	//mWorldConfigJson.clear();
	//mNodeWorkList.clear();
}
